package com.ruoyi.manage.controller;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.manage.entity.order.OrderDistributeDto;
import com.ruoyi.manage.entity.order.OrderExportDto;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manage.domain.Orders;
import com.ruoyi.manage.service.IOrdersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 所有订单Controller
 *
 * @author ruoyi
 * @date 2024-10-22
 */
@RestController
@RequestMapping("/manage/orders")
public class OrdersController extends BaseController {

    @Autowired
    private IOrdersService ordersService;

    /**
     * 查询所有订单列表
     */
    @PreAuthorize("@ss.hasPermi('manage:orders:list')")
    @GetMapping("/list")
    public TableDataInfo list(Orders orders) {
        startPage();
        List<Orders> list = ordersService.selectOrdersList(orders);
        return getDataTable(list);
    }

    /**
     * 导出所有订单列表
     */
    @PreAuthorize("@ss.hasPermi('manage:orders:export')")
    @Log(title = "所有订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Orders orders) {
        List<Orders> list = ordersService.selectOrdersList(orders);
        List<OrderExportDto> exportList = BeanUtil.copyToList(list, OrderExportDto.class);
        List<SysDictData> orderStatusList = DictUtils.getDictCache("order_status");
        assert orderStatusList != null;
        Map<String, SysDictData> dictDataMap = orderStatusList.
                stream().
                collect(Collectors.toMap(SysDictData::getDictValue, Function.identity(), (s1, s2) -> s1));
        exportList.forEach(item -> {
            String orderStatus = item.getOrderStatus();
            SysDictData sysDictData = dictDataMap.get(orderStatus);
            if (ObjectUtil.isNotNull(sysDictData)) {
                item.setOrderStatus(sysDictData.getDictLabel());
            }
            item.setOrderMoney(item.getOrderMoney().trim());
        });
        ExcelUtil<OrderExportDto> util = new ExcelUtil<>(OrderExportDto.class);
        util.exportExcel(response, exportList, "所有订单数据");
    }

    /**
     * 获取所有订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:orders:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long ID) {
        return success(ordersService.selectOrdersByID(ID));
    }

    /**
     * 新增所有订单
     */
    @PreAuthorize("@ss.hasPermi('manage:orders:add')")
    @Log(title = "所有订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Orders orders) {
        return toAjax(ordersService.insertOrders(orders));
    }

    /**
     * 修改所有订单
     */
    @PreAuthorize("@ss.hasPermi('manage:orders:edit')")
    @Log(title = "所有订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Orders orders) {
        return toAjax(ordersService.updateOrders(orders));
    }

    /**
     * 删除所有订单
     */
    @PreAuthorize("@ss.hasPermi('manage:orders:remove')")
    @Log(title = "所有订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable Long[] IDs) {
        return toAjax(ordersService.deleteOrdersByIDs(IDs));
    }

    /**
     * 订单状态修改
     * @param orders
     * @return
     */
    public AjaxResult updateStatus(@RequestBody Orders orders) {
        return null;
    }
}
